package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.text.Normalizer.Form;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.tm.model.entity.Skill;
import com.erpanderp.wam.tm.model.service.SkillDAO;
import com.erpanderp.wam.tm.model.service.SkillRatingDAO;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDetailDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ViewScoped
@ManagedBean
public class PrimarySkillsBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);	
	private WAMUser wamUser=(WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		
	private Map<String, Long> technologiesMap = new HashMap<String, Long>();
	private Map<String, Long> editableTechnologiesMap = new HashMap<String, Long>();
	private List<SAPERPModule> SAPERPModules = new ArrayList<SAPERPModule>();
	private List<Skill> skillsList = new ArrayList<Skill>();
	
    private SAPERPModuleDAO saperpModuleDAO = new SAPERPModuleDAO();
    private SAPERPModule sapERPModule=new SAPERPModule();
    private SkillRatingDAO skillRatingDAO =new SkillRatingDAO();
	
	private Skill skill =new Skill();
	SkillDAO skillDAO = new SkillDAO();
	private Long technologyId;
	private boolean FORM_VALID;
	private DataTable assignedSkillsTable;
	private boolean RENDER_CREATION_PANEL;
	
	public PrimarySkillsBean() {	
		SAPERPModules = saperpModuleDAO.getSAPERPModules();
		for (SAPERPModule saperpModule : SAPERPModules) {
			technologiesMap.put(saperpModule.getDisplayName(),saperpModule.getSAPERPModuleId());
			editableTechnologiesMap.put(saperpModule.getDisplayName(),saperpModule.getSAPERPModuleId());
			
		}
		
		skillsList = skillDAO.getSkills();
	}
	
	public void addNewSkillClicked(ActionEvent e){
		RENDER_CREATION_PANEL = true;		
	}
	
	public void saveSkill(ActionEvent e){
		validateForm();
		
		if (FORM_VALID) {
			sapERPModule = saperpModuleDAO.getSAPERPModuleBySAPERPModuleId(technologyId);
			skill.setCreateDate(new Date());
			skill.setTechnology(sapERPModule);
			skill.setCreatedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			skill.setStatus(CommonLookup.STATUS_ACTIVE);
			if(skillDAO.createSkill(skill) != null){
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "New skill created.", ""));
			}else{
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed to create skill.", ""));
			}
			skillsList.add(0, skill);
			skill = new Skill();
			technologyId = null;
			RENDER_CREATION_PANEL = false;
		}	
	}
	
	public void discardSkill(ActionEvent e){
		RENDER_CREATION_PANEL = false;
		technologyId = null;
		skill = new Skill();
	}    
	
	// VALIDATIONS
	private void validateForm() {
		FORM_VALID = true;
		if (ValidationUtil.isEmpty(skill.getSkill(), "New Skill Name", null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(skill.getDisplayName(), "Skill Display Name", null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
	}
	
	
	public void updatePrimarySkill(RowEditEvent e){
		
		System.out.println("Saving...");
		Skill selectedSkill = (Skill) e.getObject();
		FORM_VALID = true;
		
		if (ValidationUtil.isEmpty(selectedSkill.getSkill(), "Skill Name", null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if (ValidationUtil.isEmpty(selectedSkill.getDisplayName(), "Skill Display Name", null, FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		System.out.println("aaaaaaa");
		if(FORM_VALID){
			selectedSkill.setDateModified(new Date());
			selectedSkill.setModifiedBy(wamUser.getEmployee().getFirstName());	
			if(skillDAO.updateSkill(selectedSkill) != null){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Skill updated.", ""));
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Failed to update skill.", ""));
			}	
		}else{
			System.out.println("INVALID...");
		}
	}
	
   //GETTERS AND SETTERS	
	public Map<String, Long> getTechnologiesMap() {
		return technologiesMap;
	}

	public void setTechnologiesMap(Map<String, Long> technologiesMap) {
		this.technologiesMap = technologiesMap;
	}

	public List<SAPERPModule> getSAPERPModules() {
		return SAPERPModules;
	}

	public void setSAPERPModules(List<SAPERPModule> sAPERPModules) {
		SAPERPModules = sAPERPModules;
	}

	public SAPERPModuleDAO getSaperpModuleDAO() {
		return saperpModuleDAO;
	}

	public void setSaperpModuleDAO(SAPERPModuleDAO saperpModuleDAO) {
		this.saperpModuleDAO = saperpModuleDAO;
	}

	public Long getTechnologyId() {
		return technologyId;
	}

	public void setTechnologyId(Long technologyId) {
		this.technologyId = technologyId;
	}

	public Skill getSkill() {
		return skill;
	}

	public void setSkill(Skill skill) {
		this.skill = skill;
	}

	

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public SAPERPModule getSapERPModule() {
		return sapERPModule;
	}

	public void setSapERPModule(SAPERPModule sapERPModule) {
		this.sapERPModule = sapERPModule;
	}

	public List<Skill> getSkillsList() {
		return skillsList;
	}

	public void setSkillsList(List<Skill> skillsList) {
		this.skillsList = skillsList;
	}

	public DataTable getAssignedSkillsTable() {
		return assignedSkillsTable;
	}

	public void setAssignedSkillsTable(DataTable assignedSkillsTable) {
		this.assignedSkillsTable = assignedSkillsTable;
	}

	public SkillRatingDAO getSkillRatingDAO() {
		return skillRatingDAO;
	}

	public void setSkillRatingDAO(SkillRatingDAO skillRatingDAO) {
		this.skillRatingDAO = skillRatingDAO;
	}

	public boolean isRENDER_CREATION_PANEL() {
		return RENDER_CREATION_PANEL;
	}

	public void setRENDER_CREATION_PANEL(boolean rENDER_CREATION_PANEL) {
		RENDER_CREATION_PANEL = rENDER_CREATION_PANEL;
	}

	public Map<String, Long> getEditableTechnologiesMap() {
		return editableTechnologiesMap;
	}

	public void setEditableTechnologiesMap(Map<String, Long> editableTechnologiesMap) {
		this.editableTechnologiesMap = editableTechnologiesMap;
	}

}
